@extends('layout.page')
@section('title','奖池管理 - 详情')
@section('stylesheet')
    <link href="/css/ceb/gift-packs.css?v=4.1.0" rel="stylesheet">
@endsection

@section('static-page')
    <div class="col-sm-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>奖池管理 <small>编辑</small></h5>
            </div>
            <div class="ibox-content">
                <form method="post" action="/ceb/giftPacks/doEdit" class="form-horizontal">
                    {{csrf_field()}}
                    <input type="hidden" name="id" value="{{$info->id}}"/>
                    <div class="form-group  ">
                        <label class="col-sm-2 control-label">奖池名称：</label>
                        <div class="col-sm-6">
                            <div class="row">
                                <div class="col-md-8">
                                    <input type="text" name="info[name]" value="{{ $info->name }}"
                                           placeholder="奖池名称" class="form-control">
                                    <span class="help-block m-b-none">{{$errors->first('info.name')}}</span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group ">
                        <label class="col-sm-2 control-label">有效时间：</label>
                        <div class="col-sm-2 m-b-xs">
                            <div class="input-group">
                                <input placeholder="开始日期"  name="info[start_time]" value="{{ date('Y-m-d',$info->start_time)}}" class="form-control layer-date" readonly id="start">
                                <span class="help-block m-b-none"></span>
                            </div>
                        </div>
                        <div class="col-sm-2 m-b-xs">
                            <div class="input-group">
                                <input placeholder="结束日期" readonly name="info[end_time]" value="{{date('Y-m-d',$info->end_time)}}" class="form-control layer-date" id="end">
                                <span class="help-block m-b-none"></span>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-6 col-sm-offset-2">
                            <button class="btn btn-primary" type="button" id="add-xj" data-toggle="modal" data-target="#myModal-xj">添加现金券</button>
                            {{--<button class="btn btn-danger" type="button" id="add-hb" data-toggle="modal" data-target="#myModal-hb">添加红包</button>
                            --}}<button class="btn btn-warning" type="button" id="add-jx" data-toggle="modal" data-target="#myModal-jx">添加加息券</button>

                            {{--现金券开始--}}
                            <div class="modal inmodal " id="myModal-xj" tabindex="-1" role="dialog" aria-hidden="true">
                                <div class="modal-dialog ">
                                    <div class="modal-content animated bounceInRight">

                                        <div class="col-sm-12 _block-xj _block-quan">
                                            <div class="contact-box border-xj">
                                                <div class="col-sm-12">
                                                    <h3 ><strong>现金券</strong></h3>
                                                    <div class="form-group">
                                                        <label class="col-sm-4 control-label">名称：</label>
                                                        <div class="col-sm-8">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <input type="text" id="input-xj-name" value=""
                                                                           placeholder="名称" class="form-control">
                                                                    <span class="help-block m-b-none"></span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="form-group  ">
                                                        <label class="col-sm-4 control-label">面值：</label>
                                                        <div class="col-sm-8">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <input type="text" id="input-xj-val" value=""
                                                                           placeholder="面值" class="form-control">
                                                                    <span class="help-block m-b-none"></span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="form-group  ">
                                                        <div class="col-sm-12">
                                                            <h5 class="">使用条件<small></small></h5>
                                                        </div>
                                                    </div>
                                                    <div class="form-group  ">
                                                        <label class="col-sm-4 control-label">起投金额：</label>
                                                        <div class="col-sm-8">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <input type="text" id="input-xj-atleast" value=""
                                                                           placeholder="起投金额" class="form-control">
                                                                    <span class="help-block m-b-none"></span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="form-group  ">
                                                        <label class="col-sm-4 control-label">期限(天)：</label>
                                                        <div class="col-sm-8">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <input type="text" id = "input-xj-atleastDay" value=""
                                                                           placeholder="期限" class="form-control">
                                                                    <span class="help-block m-b-none"></span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="form-group  ">
                                                        <label class="col-sm-4 control-label">有效期：</label>
                                                        <div class="col-sm-8">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <div class="radio i-checks">
                                                                        <label>
                                                                            <input type="radio" checked data-val="input-xj-effectiveday" value="1" name="xjq-limitype"> <i></i> 有效天数</label>
                                                                    </div>
                                                                    <div class="radio i-checks">
                                                                        <label>
                                                                            <input type="radio"  data-val="input-xj-effectivedate" value="2" name="xjq-limitype"> <i></i> 截止日期</label>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="_eftive-container">
                                                        <div class="form-group " style="display: block">
                                                            <label class="col-sm-4 control-label">领取后有效天数(天)：</label>
                                                            <div class="col-sm-8">
                                                                <div class="row">
                                                                    <div class="col-md-12">
                                                                        <input type="text" name=""  value="" id="input-xj-effectiveday"
                                                                               placeholder="领取后有效天数(天)" class="form-control">
                                                                        <span class="help-block m-b-none"></span>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>

                                                        <div class="form-group  " style="display: none">
                                                            <label class="col-sm-4 control-label">截止日期：</label>
                                                            <div class="col-sm-8">
                                                                <div class="row">
                                                                    <div class="col-md-12">
                                                                        <input placeholder="截止日期" readonly id="input-xj-effectivedate" class="form-control layer-date">
                                                                        <span class="help-block m-b-none"></span>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="form-group">
                                                        <div class="col-sm-4 col-sm-offset-2">
                                                            <button class="btn btn-white _close-modal" type="button" data-dismiss="modal">取消</button>
                                                        </div>
                                                        <div class="col-sm-4 col-sm-offset-2">
                                                            <button class="btn btn-primary" id = "btn-add-xj" type="button">确定</button>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="clearfix"></div>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>
                            {{--END 现金券--}}

                            {{--加息券开始--}}
                            <div class="modal inmodal " id="myModal-jx" tabindex="-1" role="dialog" aria-hidden="true">
                                <div class="modal-dialog ">
                                    <div class="modal-content animated bounceInRight">

                                        <div class="col-sm-12 _block-jx _block-quan">
                                            <div class="contact-box border-jx">
                                                <div class="col-sm-12">
                                                    <h3><strong>加息券</strong></h3>
                                                    <div class="form-group">
                                                        <label class="col-sm-5 control-label">名称：</label>
                                                        <div class="col-sm-7">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <input type="text" id="input-jx-name" value=""
                                                                           placeholder="名称" class="form-control">
                                                                    <span class="help-block m-b-none"></span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="form-group  ">
                                                        <label class="col-sm-5 control-label">加息值(%)：</label>
                                                        <div class="col-sm-7">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <input type="text" id="input-jx-val" value=""
                                                                           placeholder="加息值" class="form-control">
                                                                    <span class="help-block m-b-none"></span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>


                                                    <div class="form-group  ">
                                                        <label class="col-sm-5 control-label">起投金额：</label>
                                                        <div class="col-sm-7">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <input type="text" id="input-jx-atleast" value=""
                                                                           placeholder="起投金额" class="form-control">
                                                                    <span class="help-block m-b-none"></span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="form-group  ">
                                                        <label class="col-sm-5 control-label">期限(天)：</label>
                                                        <div class="col-sm-7">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <input type="text" id="input-jx-atleastDay" value=""
                                                                           placeholder="期限" class="form-control">
                                                                    <span class="help-block m-b-none"></span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="form-group  ">
                                                        <label class="col-sm-5 control-label">有效期：</label>
                                                        <div class="col-sm-7">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <div class="radio i-checks">
                                                                        <label>
                                                                            <input type="radio" checked data-effectivtype ="1" value ="input-jx-effectiveday" name="limitType"> <i></i> 有效天数</label>
                                                                    </div>
                                                                    <div class="radio i-checks">
                                                                        <label>
                                                                            <input type="radio" data-effectivtype="2" value="input-jx-effectivedate" name="limitType"> <i></i> 截止日期</label>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="_eftive-container">
                                                        <div class="form-group" style="display: block">
                                                            <label class="col-sm-5 control-label">领取后有效天数(天)：</label>
                                                            <div class="col-sm-7">
                                                                <div class="row">
                                                                    <div class="col-md-12">
                                                                        <input type="text" id="input-jx-effectiveday" value=""
                                                                               placeholder="领取后有效天数(天)" class="form-control">
                                                                        <span class="help-block m-b-none"></span>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>

                                                        <div class="form-group" style="display: none">
                                                            <label class="col-sm-5 control-label">截止日期：</label>
                                                            <div class="col-sm-7">
                                                                <div class="row">
                                                                    <div class="col-md-12">
                                                                        <input placeholder="截止日期" readonly class="form-control layer-date" id="input-jx-effectivedate">
                                                                        <span class="help-block m-b-none"></span>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="form-group">
                                                        <div class="col-sm-4 col-sm-offset-2">
                                                            <button class="btn btn-white _close-modal" type="button" data-dismiss="modal">取消</button>
                                                        </div>
                                                        <div class="col-sm-4 col-sm-offset-2">
                                                            <button class="btn btn-primary" id="btn-add-jx" type="button">确定</button>
                                                        </div>
                                                    </div>

                                                </div>
                                                <div class="clearfix"></div>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>

                            {{--END加息券--}}

                            {{--红包开始--}}
                            <div class="modal inmodal " id="myModal-hb" tabindex="-1" role="dialog" aria-hidden="true">
                                <div class="modal-dialog ">
                                    <div class="modal-content animated bounceInRight">

                                        <div class="col-sm-12 _block-hb _block-quan">
                                            <div class="contact-box border-hb">
                                                <div class="col-sm-12">
                                                    <h3 ><strong>红包</strong></h3>
                                                    <div class="form-group  ">
                                                        <label class="col-sm-5 control-label">名称：</label>
                                                        <div class="col-sm-7">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <input type="text" id="input-hb-name" value=""
                                                                           placeholder="名称" class="form-control">
                                                                    <span class="help-block m-b-none"></span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="form-group  ">
                                                        <label class="col-sm-5 control-label">面值：</label>
                                                        <div class="col-sm-7">
                                                            <div class="row">
                                                                <div class="col-md-12">
                                                                    <input type="text" id="input-hb-val" value=""
                                                                           placeholder="面值" class="form-control">
                                                                    <span class="help-block m-b-none"></span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <div class="form-group">
                                                        <div class="col-sm-4 col-sm-offset-2">
                                                            <button class="btn btn-white _close-modal" type="button" data-dismiss="modal">取消</button>
                                                        </div>
                                                        <div class="col-sm-4 col-sm-offset-2">
                                                            <button class="btn btn-primary" id="btn-add-hb" type="button">确定</button>
                                                        </div>

                                                    </div>

                                                </div>
                                                <div class="clearfix"></div>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>
                            {{--END红包结束--}}


                        </div>
                    </div>

                    <div class="ibox-title">
                        <h5>奖池<small></small></h5>
                    </div>
                    <div class="row">
                        <div class="gift-packs-container">
                            <table class="table table-striped table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th> </th>
                                    <th>券名称</th>
                                    <th>面值</th>
                                    <th>起投金额</th>
                                    <th>起投期限</th>
                                    <th>有效期截止</th>
                                    <th>有效天数</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody class="_table-quan-container">
                                <?php $array_items = json_decode($info->items,true)?>
                                @foreach($array_items['red_packet_list'] as $key => $val)
                                    <tr class="gradeA">
                                        <td>红包</td>
                                        <td><input  class="form-control" value="{{$val['name']}}" readonly name="HB[{{$key}}][name]" ></td>
                                        <td><input  class="form-control" value="{{$val['value']}}" readonly name="HB[{{$key}}][value]"></td>
                                        <td><input  class="form-control" value="{{$val['min_investment']}}" readonly name="HB[{{$key}}][min_investment]"></td>
                                        <td><input  class="form-control" value="{{$val['duration']}}" readonly name="HB[{{$key}}][duration]"></td>
                                        <td><input  class="form-control" value="{{$val['expire_date']}}" readonly name="HB[{{$key}}][expire_date]"></td>
                                        <td><input  class="form-control" value="{{$val['effective_duration']}}" readonly name="HB[{{$key}}][effective_duration]"></td>
                                        <input  class="form-control" type="hidden" value="{{$val['expire_type']}}"  readonly name="HB[{{$key}}][expire_type]">
                                        <td class="center">
                                            <a href="javascript:void(0);" class="delete-remove">删除</a>
                                        </td>
                                    </tr>
                                @endforeach
                                @foreach($array_items['cash_voucher_list'] as $key => $val)
                                    <tr class="gradeA">
                                        <td>现金券</td>
                                        <td><input  class="form-control" value="{{$val['name']}}" readonly name="XJQ[{{$key}}][name]" ></td>
                                        <td><input  class="form-control" value="{{$val['value']}}" readonly name="XJQ[{{$key}}][value]"></td>
                                        <td><input  class="form-control" value="{{$val['min_investment']}}" readonly name="XJQ[{{$key}}][min_investment]"></td>
                                        <td><input  class="form-control" value="{{$val['duration']}}" readonly name="XJQ[{{$key}}][duration]"></td>
                                        <td><input  class="form-control" value="{{$val['expire_date']}}" readonly name="XJQ[{{$key}}][expire_date]"></td>
                                        <td><input  class="form-control" value="{{$val['effective_duration']}}" readonly name="XJQ[{{$key}}][effective_duration]"></td>
                                        <input  class="form-control" type="hidden" value="{{$val['expire_type']}}"  readonly name="XJQ[{{$key}}][expire_type]">
                                        <td class="center">
                                            <a href="javascript:void(0);" class="delete-remove">删除</a>
                                        </td>
                                    </tr>
                                @endforeach
                                @foreach($array_items['rate_increment_voucher_list'] as $key => $val)
                                    <tr class="gradeA">
                                        <td>加息券</td>
                                        <td><input  class="form-control" value="{{$val['name']}}" readonly name="JXQ[{{$key}}][name]" ></td>
                                        <td><input  class="form-control" value="{{$val['value']}}" readonly name="JXQ[{{$key}}][value]"></td>
                                        <td><input  class="form-control" value="{{$val['min_investment']}}" readonly name="JXQ[{{$key}}][min_investment]"></td>
                                        <td><input  class="form-control" value="{{$val['duration']}}" readonly name="JXQ[{{$key}}][duration]"></td>
                                        <td><input  class="form-control" value="{{$val['expire_date']}}" readonly name="JXQ[{{$key}}][expire_date]"></td>
                                        <td><input  class="form-control" value="{{$val['effective_duration']}}" readonly name="JXQ[{{$key}}][effective_duration]"></td>
                                        <input  class="form-control" type="hidden" value="{{$val['expire_type']}}"  readonly name="JXQ[{{$key}}][expire_type]">
                                        <td class="center">
                                            <a href="javascript:void(0);" class="delete-remove">删除</a>
                                        </td>
                                    </tr>
                                @endforeach

                                </tbody>
                                <tfoot>
                                <tr>
                                    <th> </th>
                                    <th>券名称</th>
                                    <th>面值</th>
                                    <th>起投金额</th>
                                    <th>起投期限</th>
                                    <th>有效期截止</th>
                                    <th>有效天数</th>
                                    <th>操作</th>
                                </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>


                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <div class="col-sm-4 col-sm-offset-2">
                            <button class="btn btn-primary" id="btn-submit" type="button">保存内容</button>
                            <a href="javascript:void(0);" onclick="window.history.back()">
                                <button class="btn btn-white" type="button">取消</button>
                            </a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

@section('javascript')

    <script>
        $(document).ready(function(){
            //礼包有效时间
            var start = {
                elem: '#start',
                format: 'YYYY-MM-DD',
                /*  min: laydate.now(),*/
                max: '2099-06-16 23:59:59',
                istime: true,
                istoday: false,
                choose: function (datas) {
                    end.min = datas; //结束日选好后，重置开始日的最大日期
                }
            };
            var end = {
                elem: '#end',
                format: 'YYYY-MM-DD',
                /*  min: laydate.now(),*/
                max: '2099-06-16 23:59:59',
                istime: true,
                istoday: false,
                choose: function (datas) {
                    start.max = datas; //结束日选好后，重置开始日的最大日期
                }
            };
            $("#start").click(function () {
                laydate(start);
            });
            $("#end").click(function () {
                laydate(end);
            });




            var ex_int = /^\d+$/;
            var count_tr = $('._table-quan-container').children('tr').length

            //添加红包
            $("#btn-add-hb").click(function () {
                var hb={};
                //券名
                if($('#input-hb-name').val().length==0){
                    $('#input-hb-name').next().html('券名不能为空');
                    $('#input-hb-name').parents('.form-group').addClass('has-error');
                    return false;
                }
                $('#input-hb-name').next().html('');
                $('#input-hb-name').parents('.form-group').removeClass('has-error');
                hb['name'] = $("#input-hb-name").val();

                //红包面值
                if($('#input-hb-val').val().length==0){
                    $('#input-hb-val').next().html('券名不能为空');
                    $('#input-hb-val').parents('.form-group').addClass('has-error');
                    return false;
                }

                //红包面值
                if(isNaN($('#input-hb-val').val())){
                    $('#input-hb-val').next().html('值必须为数子');
                    $('#input-hb-val').parents('.form-group').addClass('has-error');
                    return false;
                }
                $('#input-hb-val').next().html('');
                $('#input-hb-val').parents('.form-group').removeClass('has-error');
                hb['val'] = $("#input-hb-val").val();
                var _url= '/js/admin/ceb/gift-packs-tr-template.html?v=322';
                $.get( _url).success(function(data){
                    var _strhtml=data;
                    var reg_type=/TYPE/g;
                    _strhtml=_strhtml.replace(reg_type,'HB');
                    var reg_type=/NUMBER/g;
                    _strhtml=_strhtml.replace(reg_type,count_tr);
                    var reg_type=/TITLE/g;
                    _strhtml=_strhtml.replace(reg_type,'红包');
                    $("._table-quan-container").prepend(_strhtml);


                    $("input[name='HB["+count_tr+"][name]']").val(hb['name']);
                    $("input[name='HB["+count_tr+"][value]']").val(hb['val']);
                    count_tr++;
                    //重绑 移除事件
                    $(".delete-remove").click(function () {
                        $(this).parents('tr').remove();
                    });
                    //关闭窗口
                    $("._close-modal").click();

                });

            });
            //添加加息券
            $("#btn-add-jx").click(function () {

                var jxq={};
                //券名
                if($('#input-jx-name').val().length==0){
                    $('#input-jx-name').next().html('券名不能为空');
                    $('#input-jx-name').parents('.form-group').addClass('has-error');
                    return false;
                }
                $('#input-jx-name').next().html('');
                $('#input-jx-name').parents('.form-group').removeClass('has-error');
                //加息值
                if($("#input-jx-val").val().length==0){
                    $('#input-jx-val').next().html('加息值不能为空');
                    $('#input-jx-val').parents('.form-group').addClass('has-error');
                    return false;
                }
                if(isNaN($("#input-jx-val").val())){
                    $('#input-jx-val').next().html('加息值必须为数字');
                    $('#input-jx-val').parents('.form-group').addClass('has-error');
                    return false;
                }
                $('#input-jx-val').next().html('');
                $('#input-jx-val').parents('.form-group').removeClass('has-error');

                //起投金额
                if($("#input-jx-atleast").val().length>0 && !ex_int.test($("#input-jx-atleast").val())){
                    $('#input-jx-atleast').next().html('起投金额必须为整数');
                    $('#input-jx-atleast').parents('.form-group').addClass('has-error');
                    return false;
                }
                $('#input-jx-atleast').next().html('');
                $('#input-jx-atleast').parents('.form-group').removeClass('has-error');

                //期限
                if($("#input-jx-atleastDay").val().length>0 && !ex_int.test($("#input-jx-atleastDay").val())){
                    $('#input-jx-atleastDay').next().html('期限必须为整数');
                    $('#input-jx-atleastDay').parents('.form-group').addClass('has-error');
                    return false;
                }
                $('#input-jx-atleastDay').next().html('');
                $('#input-jx-atleastDay').parents('.form-group').removeClass('has-error');

                var effectiveId = $("input[name='limitType']").val();
                if(effectiveId =='input-jx-effectiveday' && $("#"+effectiveId).val().length>0 ){
                    if(!ex_int.test($("#"+effectiveId).val())){
                        $("#"+effectiveId).next().html('有效天数必须为整数');
                        $("#"+effectiveId).parents('.form-group').addClass('has-error');
                        return false;
                    }
                }
                $("#"+effectiveId).next().html('');
                $("#"+effectiveId).parents('.form-group').removeClass('has-error');

                var _url= '/js/admin/ceb/gift-packs-tr-template.html?v=322';
                $.get( _url).success(function(data){
                    var _strhtml=data;
                    var reg_type=/TYPE/g;
                    _strhtml=_strhtml.replace(reg_type,'JXQ');
                    var reg_type=/NUMBER/g;
                    _strhtml=_strhtml.replace(reg_type,count_tr);
                    var reg_type=/TITLE/g;
                    _strhtml=_strhtml.replace(reg_type,'加息券');
                    $("._table-quan-container").prepend(_strhtml);

                    $("input[name='JXQ["+count_tr+"][name]']").val($('#input-jx-name').val());
                    $("input[name='JXQ["+count_tr+"][value]']").val($('#input-jx-val').val());
                    $("input[name='JXQ["+count_tr+"][min_investment]']").val($('#input-jx-atleast').val());
                    $("input[name='JXQ["+count_tr+"][duration]']").val($("#input-jx-atleastDay").val());
                    $("input[name='JXQ["+count_tr+"][expire_type]']").val($("input[name='limitType']:checked").data('effectivtype'));
                    if($("input[name='limitType']:checked").data('effectivtype')==1){
                        $("input[name='JXQ["+count_tr+"][effective_duration]']").val($("#input-jx-effectiveday").val());
                    }
                    else{
                        $("input[name='JXQ["+count_tr+"][expire_date]']").val($("#input-jx-effectivedate").val());
                    }
                    console.log($("input[name='limitType']:checked").data('effectivtype'));
                    count_tr++;
                    //重绑 移除事件
                    $(".delete-remove").click(function () {
                        $(this).parents('tr').remove();
                    });
                    //关闭窗口
                    $("._close-modal").click();

                });

            });

            //添加现金券
            $("#btn-add-xj").click(function () {
                var xjq={};
                if($("#input-xj-name").val().length==0){
                    $('#input-xj-name').next().html('券名不能为空');
                    $('#input-xj-name').parents('.form-group').addClass('has-error');
                    return false;
                }
                $('#input-xj-name').next().html('');
                $('#input-xj-name').parents('.form-group').removeClass('has-error');
                xjq['name'] = $("#input-xj-name").val();

                if( $("#input-xj-val").val().length == 0 || isNaN($('#input-xj-val').val())){
                    $('#input-xj-val').next().html('值必为数字');
                    $('#input-xj-val').parents('.form-group').addClass('has-error');
                    return false;
                }
                $('#input-xj-val').next().html('');
                $('#input-xj-val').parents('.form-group').removeClass('has-error');
                xjq['val'] =  $('#input-xj-val').val();


                if($("#input-xj-atleast").val().length>0 && !ex_int.test($("#input-xj-atleast").val())){
                    $("#input-xj-atleast").next().html('值必须为数字');
                    $("#input-xj-atleast").parents('.form-group').addClass('has-error');
                    return false;
                }
                $("#input-xj-atleast").next().html('');
                $("#input-xj-atleast").parents('.form-group').removeClass('has-error');
                xjq['atleast'] =  $("#input-xj-atleast").val();

                if($("#input-xj-atleastDay").val().length>0 && !ex_int.test($("#input-xj-atleastDay").val())){
                    $("#input-xj-atleastDay").next().html('值必须为数字');
                    $("#input-xj-atleastDay").parents('.form-group').addClass('has-error');
                    return false;
                }
                $("#input-xj-atleastDay").next().html('');
                $("#input-xj-atleastDay").parents('.form-group').removeClass('has-error');
                xjq['atleastDay'] =  $("#input-xj-atleastDay").val();

                xjq['limitType'] = $("input[name='xjq-limitype']:checked").val();
                if(xjq['limitType']==1){
                    xjq['effective_duration'] = $("#input-xj-effectiveday").val();
                    if(xjq['effective_duration'].length>0 && !ex_int.test(xjq['effective_duration'])){
                        $("#input-xj-effectiveday").next().html('值必须为数字');
                        $("#input-xj-effectiveday").parents('.form-group').addClass('has-error');
                        return false;
                    }
                }
                else{
                    xjq['expire_date'] = $("#input-xj-effectivedate").val();
                }

                var _url= '/js/admin/ceb/gift-packs-tr-template.html?v=322';
                $.get( _url).success(function(data){
                    var _strhtml=data;
                    var reg_type=/TYPE/g;
                    _strhtml=_strhtml.replace(reg_type,'XJQ');
                    var reg_type=/NUMBER/g;
                    _strhtml=_strhtml.replace(reg_type,count_tr);
                    var reg_type=/TITLE/g;
                    _strhtml=_strhtml.replace(reg_type,'现金券');
                    $("._table-quan-container").prepend(_strhtml);

                    $("input[name='XJQ["+count_tr+"][name]']").val(xjq['name']);
                    $("input[name='XJQ["+count_tr+"][value]']").val(xjq['val']);
                    $("input[name='XJQ["+count_tr+"][min_investment]']").val(xjq['atleast']);
                    $("input[name='XJQ["+count_tr+"][duration]']").val(xjq['atleastDay']);
                    $("input[name='XJQ["+count_tr+"][expire_type]']").val(xjq['limitType']);
                    $("input[name='XJQ["+count_tr+"][expire_date]']").val(xjq['expire_date']);
                    $("input[name='XJQ["+count_tr+"][effective_duration]']").val(xjq['effective_duration']);
                    console.log($("input[name='limitType']:checked").data('effectivtype'));
                    count_tr++;
                    //重绑 移除事件
                    $(".delete-remove").click(function () {
                        $(this).parents('tr').remove();
                    });
                    //关闭窗口
                    $("._close-modal").click();

                });


            });

            //加息chebox 点击
            $("input[name='limitType']").change(function () {
                var docment = $("#"+$(this).val()).parents('.form-group');
                docment.show().siblings().hide();
            });
            //现金券chebox 点击
            $("input[name='xjq-limitype']").change(function () {
                var docment = $("#"+$(this).data('val')).parents('.form-group');
                docment.show().siblings().hide();
            });

            //移除事件
            $(".delete-remove").click(function () {
                $(this).parents('tr').remove();
            });


            //加息时间控件
            var time_modle_jx = {
                elem: '#input-jx-effectivedate',
                format: 'YYYY-MM-DD',
                min: laydate.now(),
                max: '2099-06-16 23:59:59',
                istime: true,
                istoday: false,
                choose: function (datas) {
                    //start.max = datas; //结束日选好后，重置开始日的最大日期
                }
            };
            $("#input-jx-effectivedate").click(function () {
                laydate(time_modle_jx);
            });

            //现金券时间控件
            var time_modle_xj = {
                elem: '#input-xj-effectivedate',
                format: 'YYYY-MM-DD',
                min: laydate.now(),
                max: '2099-06-16 23:59:59',
                istime: true,
                istoday: false,
                choose: function (datas) {
                    //start.max = datas; //结束日选好后，重置开始日的最大日期
                }
            };
            $("#input-xj-effectivedate").click(function () {
                laydate(time_modle_xj);
            });

            //提交验证
            $("#btn-submit").click(function () {
                var doc_name = $("input[name='info[name]']");
                if( doc_name.val().length == 0){
                    doc_name.next().html("值不能为空");
                    doc_name.parents('.form-group').addClass('has-error');
                    return false;
                }
                doc_name.next().html("");
                doc_name.parents('.form-group').removeClass('has-error');

                if($("#start").val().length == 0){
                    $("#start").next().html('值不能为空');
                    $("#start").parents('.form-group').addClass('has-error');
                    return false;
                }
                $("#start").next().html('');
                $("#start").parents('.form-group').removeClass('has-error');

                if($("#end").val().length == 0){
                    $("#end").next().html('值不能为空');
                    $("#end").parents('.form-group').addClass('has-error');
                    return false;
                }
                $("#end").next().html('');
                $("#end").parents('.form-group').removeClass('has-error');

                if($("._table-quan-container").children('tr').length == 0){
                    layer.msg("礼包不能空", {icon: 5});
                    return false;
                }
                $("form").submit();

            });

        });
    </script>
@endsection
